<template>
  <el-dropdown trigger="click" @command="onLanguageChange">
    <div class="lang-select--style">
      <svg-icon icon-class="language" />
    </div>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item :disabled="appStore.language === 'zh-cn'" command="zh-cn"> 中文 </el-dropdown-item>
        <el-dropdown-item :disabled="appStore.language === 'en'" command="en"> English </el-dropdown-item>
        <el-dropdown-item :disabled="appStore.language === 'km'"   command="km">ភាសា​ខ្មែរ</el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script setup lang="ts">
import { useI18n } from 'vue-i18n';
import { useAppStore } from '@/store/modules/app';
import SvgIcon from '@/components/SvgIcon/index.vue';
import {Local} from "@/utils/storage";
import other from "@/utils/other";
import { useThemeConfig } from '@/store/themeConfig';

const appStore = useAppStore();
const { locale } = useI18n();

const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);

const message: any = {
  zh_CN: '切换语言成功！',
  en: 'Switch Language Successful!',
  km: 'ប្ដូរ​ភាសា​ដោយ​ជោគជ័យ!'
};



// 语言切换
const onLanguageChange = (lang: string) => {
  Local.remove('themeConfig');
  themeConfig.value.globalI18n = lang;
  Local.set('themeConfig', themeConfig.value);
  console.log('themeConfig.value', themeConfig.value)
  locale.value = lang;
  other.useTitle();
  appStore.changeLanguage(lang);
  ElMessage.success(message[lang] || '切换语言成功！');
};


const handleLanguageChange = (lang: any) => {
  locale.value = lang;
  appStore.changeLanguage(lang);
  ElMessage.success(message[lang] || '切换语言成功！');
};
</script>

<style lang="scss" scoped>
.lang-select--style {
  font-size: 18px;
  line-height: 50px;
}
</style>
